<?php echo CHtml::beginForm(Yii::app()->controller->createUrl('deal/uploadImg'),'post',array('id' => 'uploadImgForm')); //'enctype'=>'multipart/form-data'?> 
	<?php echo CHtml::activeFileField($model,'image', array('id'=>'ajax_upl_im_field')); ?>
	<?php echo CHtml::hiddenField('deal_id', $dealId); ?>
	<?php echo CHtml::script("
		 	$('#uploadImgForm').ajaxForm({
		        dataType: 'json',
		    	success: function(transport) { uploadImgSuccess(transport); return false;},
		    	error: function(transport) { uploadImgError(transport); return false; }
			});");
		//alert(transport.toSource()); return false; 
	?>
	<input type="submit" value="Submit" />
<?php echo CHtml::endForm(); ?>

<script type="text/javascript">
/* <![CDATA[ */
	function uploadImgSuccess(transport){
		if(transport && transport.errorSummary) {
			alert(transport.errorSummary);
			return false;
		}
		// @TODO refactoring thos code see _images.php
		if(transport && transport.success){
			var html = '<div id="deal_img_'+ transport.success.id +'">';
			html = html + '<img width="180" style="margin: 0.2em 0 0.5em;" height="180" alt="image" src="/uploads/images/thumbnail_180_180_p/'+ transport.success.name +'" />';
			html = html + '<a href="javascript: void(0);" title="Удалить" class="delete" onclick="deleteImg('+ transport.success.id +')">';
			html = html + '<img alt="Удалить" src="/images/delete.png">';
			html = html + '</a>';
			html = html + '</div>';
			
			$('#upload_img_button').before(html);
			$("#upload_deal_image").dialog("close");	
		}
	}

	function uploadImgError(transport){
		$("#upload_deal_image").dialog("close");
		return false;
		
		alert(transport.toSource());
		if(transport.responseText) {
			$("#uploadImgForm").before(transport.responseText);
			return false;
		} else {
			$("#upload_deal_image").dialog("close");
		} 
		
		if (transport.status != 200) {
			alert('Sorry, but some error occured. Try again please');
		}	
	}

	
/* ]]> */
</script>
